/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package resource;

import com.github.dvdme.ForecastIOLib.FIODaily;
import com.github.dvdme.ForecastIOLib.FIODataPoint;
import com.github.dvdme.ForecastIOLib.ForecastIO;
import com.winterwell.jgeoplanet.GeoPlanet;
import com.winterwell.jgeoplanet.GeoPlanetException;
import com.winterwell.jgeoplanet.Place;
import java.util.Calendar;

/**
 *
 * @author Luigi
 */
public class WeatherWrapper {
    
    //necessary attributes
    private Double latitude;
    private Double longitude;
    private String city; 
    private ForecastIO weather;         //This is the real weather object
    private FIODaily daily;             //This is the daily forecast
    
    //Methods
        //Functions that updates every field when a city is inserted
    public void updateFromCity(){
        //Initializing GeoPlanet. GeoPlanet is a utility library for Yahoo! weather services
        //We need it only to do an operation: find the id of a city, having its name
        //(the users don't have the id for each city... stupid users) 
        //UPDATE: considering the forecast, the main purpose of this library is to provide 
        //coordinates from a city
        GeoPlanet client;
        try {
            //A key is needed to connect to Yahoo! Weather services. That's personal and linked to Luigi's Yahoo! account.
            client = new GeoPlanet("2po7Oo_V34FxY_XLnG.cqZlEqst1E2XBdNc5vWd8eVXz7_V9tkm6QB7qrh7T2z3r22FlzmE82iQ-");
        } catch (GeoPlanetException ex) {
            this.setCity("NOT ABLE TO CONNECT TO YAHOO WEATHER SERVICES");
            return;
        }
        Place placeInserted;
        try {
            //Getting the GeoPlanet Place of the specified city
            placeInserted = client.getPlace(this.getCity());
        } catch (GeoPlanetException ex) {
            this.setCity("NOT A VALID CITY!");
            return;
        }
        this.setLatitude((Double) placeInserted.getCentroid().getLatitude());
        this.setLongitude((Double) placeInserted.getCentroid().getLongitude());
        
        //Now I create the weather object
        //Yes, we're using another library, with the service Forecast.io
        ForecastIO fio = new ForecastIO("7ca983413ba16e2d1d15c95cabce2bb2"); 
        fio.setUnits(ForecastIO.UNITS_SI);             //sets the units as SI - optional
        fio.setExcludeURL("hourly,minutely");             //excluded the minutely and hourly reports from the reply
        fio.getForecast(this.getLatitude().toString(), this.getLongitude().toString());
        
        //And finally the weather daily forecast
        this.setDaily(new FIODaily(fio));
        
    }
    
    //This function give us the daily weather object of a desired day of the month
    public FIODataPoint getSpecificDay(int desiredDay, int currentDay){
        //Check if we have data for that day
       
        int lastCoveredDay=currentDay+this.getDaily().days();
        if ((desiredDay<currentDay)||(desiredDay>lastCoveredDay)) return null;
        return getDaily().getDay(desiredDay-currentDay);
    }
    //If we don't have the currentDay I will find it
    public FIODataPoint getSpecificDay(int desiredDay){
        Calendar cal = Calendar.getInstance();
        int currentDay = cal.get(Calendar.DAY_OF_MONTH);
        return getSpecificDay(desiredDay,currentDay);
    }
    /**
     * @return the latitude
     */
    public Double getLatitude() {
        return latitude;
    }

    /**
     * @param latitude the latitude to set
     */
    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    /**
     * @return the longitude
     */
    public Double getLongitude() {
        return longitude;
    }

    /**
     * @param longitude the longitude to set
     */
    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        if (city==null) city="Milano"; //fallback;
        this.city = city;
        updateFromCity();
    }
    
    //Default setter
    /*public void setCity(){
        this.city="Milano";
         updateFromCity();
    }*/

    /**
     * @return the weather
     */
    public ForecastIO getWeather() {
        return weather;
    }

    /**
     * @param weather the weather to set
     */
    public void setWeather(ForecastIO weather) {
        this.weather = weather;
    }

    /**
     * @return the daily
     */
    public FIODaily getDaily() {
        return daily;
    }

    /**
     * @param daily the daily to set
     */
    public void setDaily(FIODaily daily) {
        this.daily = daily;
    }
    
}
